﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System8Equations;
using System.ComponentModel;

namespace System8ToyMC
{
	public partial class RVNormalDistribution : UserControl
	{
		public RVNormalDistribution()
		{
			// Required to initialize variables
			InitializeComponent();
            LayoutRoot.DataContext = this;
		}

        /// <summary>
        /// Get/Set the description of this guy
        /// </summary>
        private string Description
        {
            get { return (string)GetValue(DescriptionProperty); }
            set { SetValue(DescriptionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Description.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DescriptionProperty =
            DependencyProperty.Register("Description", typeof(string), typeof(RVNormalDistribution), new PropertyMetadata(""));


        /// <summary>
        /// Get/Set the variable we are showing the world
        /// </summary>
        [Description("The S8Value that we are going to display")]
        [Category("Distribution")]
        public S8Variable Value
        {
            get { return (S8Variable)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(S8Variable), typeof(RVNormalDistribution), new PropertyMetadata(null, (o, a) => (o as RVNormalDistribution).UpdateValue()));

        /// <summary>
        /// The variable has updated - make sure the description UI knows about it!
        /// </summary>
        private void UpdateValue()
        {
            Value.ValueChanged += new Action<S8Variable>(Value_ValueChanged);
            UpdateDescription();
        }

        /// <summary>
        /// Update the description - but do it in the UI thread!
        /// </summary>
        private void UpdateDescription()
        {
            Dispatcher.BeginInvoke(() => Description = Value.ToString());
        }

        /// <summary>
        /// Something about the value we are watching changed.
        /// </summary>
        /// <param name="obj"></param>
        void Value_ValueChanged(S8Variable obj)
        {
            UpdateDescription();
        }
	}
}